﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GraphicLib;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace GraphicIdentification
{
    public partial class frmProductsManage : Form
    {
        public frmProductsManage()
        {
            InitializeComponent();
            DeserializeProductInfo();

        }

        /// <summary>
        /// 序列化当前的样本数据
        /// </summary>
        private void SerializeProductInfo()
        {
            using (FileStream fs = new FileStream(Sample.PRODUCT_INFO_DATA_PATH, FileMode.Create))
            {
                BinaryFormatter formatter = new BinaryFormatter();
                List<ListViewItem> items = new List<ListViewItem>();
                foreach (ListViewItem item in lstProducts.Items)
                {
                    items.Add(item);
                }
                formatter.Serialize(fs, items);
            }
        }

        /// <summary>
        /// 反序列化已存在的样本数据
        /// </summary>
        public void DeserializeProductInfo()
        {
            if (File.Exists(Sample.PRODUCT_INFO_DATA_PATH))
            {
                using (FileStream fs = new FileStream(Sample.PRODUCT_INFO_DATA_PATH, FileMode.Open))
                {
                    BinaryFormatter formatter = new BinaryFormatter();
                    List<ListViewItem> items = (List<ListViewItem>) formatter.Deserialize(fs);
                    foreach (ListViewItem item in items)
                    {
                        lstProducts.Items.Add(item);
                    }
                }
            }
        }

        private string newProductName;

        public string NewProductName
        {
            get { return newProductName; }
            set { newProductName = value; }
        }

        private void btnAddProduct_Click(object sender, EventArgs e)
        {
            frmProductAdd productAdd = new frmProductAdd();
            productAdd.Owner = this;
            productAdd.ShowDialog();
            if (this.NewProductName != "")
            {
                if (productAdd.DialogResult == DialogResult.OK)
                {
                    lstProducts.Items.Add(newProductName);
                }
                SerializeProductInfo();
                CreateProductDir();
            }
            else
            {
                MessageBox.Show("产品名称不能为空！");             
            }
        }

        /// <summary>
        /// 创建全套产品目录（包含Bad_Products/Good_Products/InitializedSamples/BadProductsLearning/Products/Samples）
        /// </summary>
        private void CreateProductDir()
        {
            string[] dirs = new string[] { "Bad_Products", "Good_Products", "InitializedSamples", "BadProductsLearning", "Products", "Samples" };
            foreach (string dir in dirs)
            {
                FileUtils.CreateDir(newProductName + @"\" + dir);
            }
        }

        private void btnDeleteProduct_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in lstProducts.SelectedItems)
            {
                lstProducts.Items.Remove(item);
            }
            SerializeProductInfo();
        }

        private void btnChooseProduct_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in lstProducts.SelectedItems)
            {
                Sample.SELECTED_PRODUCT = item.Text;
            }
            SamplesInition.DeserializeSample();
            //MessageBox.Show("已选定产品" + Sample.SELECTED_PRODUCT);
            this.DialogResult = DialogResult.OK;            
        }
    }
}
